<?php
		global $db;
    
    // get module title
		// because if the client switch page, so, that is the action and exponent does not transfer $title and $views		
	  $temp=$db->selectObject("container","internal = '" . serialize($loc) . "'");
	  if ($temp)
	  {
	    $title=$temp->title;
	    $view=$temp->view;
    }
    else
    {
      $title="";
      $view="Default";
    }
    		
		$config = $db->selectObject('addressbookmodule_config',"location_data='".serialize($loc)."'");
		if (!$config) {
			$config->sort_type = 'lastname_asc';
		}
		$max_item=50;
		$count_post = $db->countObjects('addressbook_contact',"location_data='".serialize($loc)."'");
		// page split
		// cal how many posts
		$page_count=floor($count_post / $max_item);
		if ($count_post % $max_item != 0)
		  $page_count++;
		
		// get page
		$page=1;
		if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
    if (($page<1) || ($page>$page_count)) $page=1;
    $start_pos=($page-1)*$max_item;
    //
		
		$contacts = array();
		foreach ($db->selectObjects("addressbook_contact","location_data='".serialize($loc)."' LIMIT {$start_pos},{$max_item}") as $c) {
			$contacts[$c->id] = $c;
			
			$iloc = exponent_core_makeLocation($loc->mod,$loc->src,$c->id);
			$contacts[$c->id]->permissions = array(
				'administrate'=>exponent_permissions_check('administrate',$iloc),
				'edit'=>exponent_permissions_check('edit',$iloc),
				'delete'=>exponent_permissions_check('delete',$iloc)
			);
			// 10/04/09: immanuel192 add this line code
			// for image path
			if ($c->image > 0){
			   $temp=$db->selectObject('file',"id = {$c->image}");
			   if ($temp)
			     $contacts[$c->id]->image_path=$temp->directory."/".$temp->filename;
			  else 
			     $contacts[$c->id]->image_path='';
      }
		}
		// Sorting
		if (!defined('SYS_SORTING')) require_once(BASE.'subsystems/sorting.php');
		if (!function_exists('exponent_sorting_byFirstNameAscending')) {
			function exponent_sorting_byFirstNameAscending($a,$b) {
				return strnatcmp($a->firstname ,$b->firstname);
			}
		}
		if (!function_exists('exponent_sorting_byFirstNameDescending')) {
			function exponent_sorting_byFirstNameDescending($a,$b) {
				return -1*strnatcmp($a->firstname ,$b->firstname);
			}
		}
		if (!function_exists('exponent_sorting_byLastNameAscending')) {
			function exponent_sorting_byLastNameAscending($a,$b) {
				return strnatcmp($a->lastname ,$b->lastname);
			}
		}
		if (!function_exists('exponent_sorting_byLastNameDescending')) {
			function exponent_sorting_byLastNameDescending($a,$b) {
				return -1*strnatcmp($a->lastname ,$b->lastname);
			}
		}
		
		switch ($config->sort_type) {
			case 'lastname_asc':
				usort($contacts,'exponent_sorting_byLastNameAscending');
				break;
			case 'lastname_desc':
				usort($contacts,'exponent_sorting_byLastNameDescending');
				break;
			case 'firstname_asc':
				usort($contacts,'exponent_sorting_byFirstNameAscending');
				break;
			case 'firstname_desc':
				usort($contacts,'exponent_sorting_byFirstNameDescending');
				break;
		}
		
		$template = new template('addressbookmodule',$view,$loc);
		
		$template->assign('contacts',$contacts);
		$template->assign('moduletitle',$title);
		$template->assign('pages',$page_count);
		$template->assign('page',$page);
		$template->register_permissions(
			array('administrate','configure','post','edit','delete'),
			$loc
		);
		
		$template->output();
?>
